
    /**  
    * @Title: DeliveryUtil.java
    * @Package com.third.kuaidi
    * @Description: 
    * @author likai
    * @date 2017年8月19日
    * @version V1.0  
    */
    
package com.third.utils.kuaidi100;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.supers.enums.Charset;
import com.third.base.kuaidi100.MD5;
import com.third.base.kuaidi100.TrackResult;
import com.utils.http.HttpClientUtil;
//import com.utils.http.HttpRequest;
import com.utils.properties.PropertyUtil;
import com.utils.transtype.JsonObjectUtil;
import com.utils.validate.RegexUtil;

/**
    * @ClassName: DeliveryUtil
    * @Description: 快递100工具类  网站：http://www.kuaidi100.com/openapi/
    * @author likai
    * @date 2017年8月19日
    *
    */

public class DeliveryUtil{
	public static Logger log = LoggerFactory.getLogger(DeliveryUtil.class);
	private String customer;//账号
	private String key;//密码
	private Properties prop;
	private static DeliveryUtil deliveryUtil;
	
	public static DeliveryUtil init(String propPath) {
		if (deliveryUtil==null) {
			deliveryUtil=new DeliveryUtil(propPath);
		}
		return deliveryUtil;
	}
	public static DeliveryUtil init(DeliveryConfig deliveryConfig) {
		
		if (deliveryUtil==null) {
			deliveryUtil=new DeliveryUtil(deliveryConfig);
		}
		return deliveryUtil;
	}
	private DeliveryUtil(String propPath) {
		super();
		prop=PropertyUtil.getProp(propPath);
		customer=prop.getProperty("customer");
		key=prop.getProperty("key");
	}
	private DeliveryUtil(DeliveryConfig deliveryConfig) {
		super();
		customer=deliveryConfig.getCustomer();
		key=deliveryConfig.getKey();
	}

	/* 
	    * @Description:快递100实时快递查询接口
	    * 
	    * 订单编号：num
	    *
	    */
	
	public TrackResult query(String num) {
		TrackResult result=null;
		String com=autonumber(num);
		String param ="{\"com\":\""+com+"\",\"num\":\""+num+"\"}";
		String sign = MD5.encode(param+key+customer);
		Map<String, String> params = new HashMap<String, String>();
		params.put("param",param);
		params.put("sign",sign);
		params.put("customer",customer);
		try {
			String response=(String) HttpClientUtil.httpForm.postClient("http://poll.kuaidi100.com/poll/query.do", params,  Charset.UTF8,String.class );
			//String response= HttpRequest.postData("http://poll.kuaidi100.com/poll/query.do", params, "utf-8").toString();
			response=RegexUtil.replaceTelnum(response);
			result=JsonObjectUtil.ALWAYS.jsonToObject(response, TrackResult.class);
			result.setCom(com);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("快递单号【"+num+"】查询快物流息失败:"+e.getMessage());
		}
		return result;
	}
	/* 
	    * @Description:快递100实时快递查询接口
	    * 
	    * 订单编号：num
	    *
	    */
	
	public String queryString(String num) throws Exception {
		String com=autonumber(num);
		String param ="{\"com\":\""+com+"\",\"num\":\""+num+"\"}";
		String sign = MD5.encode(param+key+customer);
		Map<String, String> params = new HashMap<String, String>();
		params.put("param",param);
		params.put("sign",sign);
		params.put("customer",customer);
		String response=(String) HttpClientUtil.httpForm.postClient("http://poll.kuaidi100.com/poll/query.do", params,  Charset.UTF8,String.class );
		//String response= HttpRequest.postData("http://poll.kuaidi100.com/poll/query.do", params, "utf-8").toString();
		return response;
	}
	
	/* 
	    * @Description:快递100单号归属公司智能判断接口(获取快递公司名称)
	    * 
	    * 订单编号：num
	    *
	    */
	
	@SuppressWarnings("unchecked")
	public String autonumber(String num) {
		try {
			String url="http://www.kuaidi100.com/autonumber/auto?num="+num+"&key="+key;
			List<Map<String, Object>> list=  (List<Map<String, Object>>) HttpClientUtil.httpJson.getClient(url, Charset.UTF8, List.class);
			return (String) list.get(0).get("comCode");
		} catch (Exception e) {	
			log.error("快递单号【"+num+"】查询归属公司失败:"+e.getMessage());
			return null;
		}
	}
	
}
